package com.samphanie.auiu;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import com.samphanie.auiu.common.ui.component.AuiuBaseActivity;
import com.samphanie.auiu.library.utils.AuiuStatusBar;
import com.samphanie.auiu.logic.MainActivityLogic;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class MainActivity extends AuiuBaseActivity implements MainActivityLogic.ActivityProvider {

    private MainActivityLogic activityLogic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activityLogic = new MainActivityLogic(this, savedInstanceState);

        // startActivity(new Intent(this, LoginActivity.class));
        AuiuStatusBar.INSTANCE.setStatusBar(this, true, Color.WHITE, false);
        // Toast.makeText(this, "测试测试测试", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            // 音量下键点击事件
            if (BuildConfig.DEBUG) {
                try {
                    Class<?> aClass = Class.forName("com.samphanie.auiu.debug.DebugToolDialogFragment");
                    try {
                        DialogFragment target = (DialogFragment) aClass.getConstructor().newInstance();
                        target.show(getSupportFragmentManager(), "debug_tool");
                    } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) {
                        e.printStackTrace();
                    }

                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        activityLogic.onSaveInstanceState(outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}